package org.jeecg.modules.flowermater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.flowermater.entity.FlowerMaterialBook;
import org.jeecg.modules.flowermater.entity.FlowerMaterialInMain;
import org.jeecg.modules.flowermater.entity.FlowerMaterialIn;
import org.jeecg.modules.flowermater.entity.FlowerMaterialOutMain;
import org.jeecg.modules.flowermater.mapper.FlowerMaterialBookMapper;
import org.jeecg.modules.flowermater.mapper.FlowerMaterialInMapper;
import org.jeecg.modules.flowermater.mapper.FlowerMaterialInMainMapper;
import org.jeecg.modules.flowermater.service.IFlowerMaterialBookService;
import org.jeecg.modules.flowermater.service.IFlowerMaterialInMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采后物资入库主表
 * @Author: jeecg-boot
 * @Date:   2022-07-13
 * @Version: V1.0
 */
@Service
public class FlowerMaterialInMainServiceImpl extends ServiceImpl<FlowerMaterialInMainMapper, FlowerMaterialInMain> implements IFlowerMaterialInMainService {

	@Autowired
	private FlowerMaterialInMainMapper flowerMaterialInMainMapper;
	@Autowired
	private FlowerMaterialInMapper flowerMaterialInMapper;
	@Autowired
	private FlowerMaterialBookMapper  flowerMaterialBookMapper;
	@Autowired
	private IFlowerMaterialBookService flowerMaterialBookService;


	@Override
	@Transactional
	public void saveMain(FlowerMaterialInMain flowerMaterialInMain, List<FlowerMaterialIn> flowerMaterialInList) {
		flowerMaterialInMain.setState(0);
		String supplier = flowerMaterialInMain.getSupplier();

		flowerMaterialInMainMapper.insert(flowerMaterialInMain);
		if(flowerMaterialInList!=null && flowerMaterialInList.size()>0) {
			for(FlowerMaterialIn entity:flowerMaterialInList) {
				//外键设置
				entity.setMainid(flowerMaterialInMain.getId());
				if(StringUtils.isNotBlank(supplier )){
					entity.setSupplier( supplier);
				}
				flowerMaterialInMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FlowerMaterialInMain flowerMaterialInMain,List<FlowerMaterialIn> flowerMaterialInList) {
		String mainId = flowerMaterialInMain.getId();
		String supplier = flowerMaterialInMain.getSupplier();
		if(StringUtils.isNotBlank(flowerMaterialInMain.getHouseSureUser())){
			throw new RuntimeException("库管已经确认！请先取消");
		}
		if(StringUtils.isNotBlank(flowerMaterialInMain.getFinaSureUser())){
			throw new RuntimeException("财务已经复核确认！请先取消");
		}

		if(flowerMaterialInMain.getState()==2){
			throw new RuntimeException("财务已经审核，不能修改");
		}
		if(flowerMaterialInMain.getState()==1){
			throw new RuntimeException("库管已经审核，不能修改");
		}
		if(flowerMaterialInMain.getState()!=0){
			throw new RuntimeException("不能修改");
		}


		flowerMaterialInMainMapper.updateById(flowerMaterialInMain);
		//1.先删除子表数据
		flowerMaterialInMapper.deleteByMainId(flowerMaterialInMain.getId());
		//2.子表数据重新插入
		if(flowerMaterialInList!=null && flowerMaterialInList.size()>0) {
			for(FlowerMaterialIn entity:flowerMaterialInList) {
				//外键设置
				entity.setMainid(flowerMaterialInMain.getId());
				if(StringUtils.isNotBlank(supplier )){
					entity.setSupplier( supplier);
				}
				flowerMaterialInMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		String mainId = id;
		FlowerMaterialInMain flowerMaterialInMain = this.getById(mainId);
		if(flowerMaterialInMain.getState()==2){
			throw new RuntimeException("财务已经审核，不能删除");
		}
		if(flowerMaterialInMain.getState()==1){
			throw new RuntimeException("库管已经审核，不能删除");
		}
		if(flowerMaterialInMain.getState()!=0){
			throw new RuntimeException("不能删除");
		}

		flowerMaterialInMapper.deleteByMainId(id);
		flowerMaterialInMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			FlowerMaterialInMain materialInMain = this.getById(id);
			if(materialInMain.getState()==2){
				throw new RuntimeException("财务已经审核，不能删除");
			}
			if(materialInMain.getState()==1){
				throw new RuntimeException("库管已经审核，不能删除");
			}
			if(materialInMain.getState()!=0){
				throw new RuntimeException("不能删除");
			}

			flowerMaterialInMapper.deleteByMainId(id.toString());
			flowerMaterialInMainMapper.deleteById(id);
		}
	}
	/**
	 *
	 * 采后物资入库主表-库管确认
	 * @param flowerMaterialInMain
	 * @param flowerMaterialInList
	 */
	@Override
	@Transactional
	public void updateBookSure(FlowerMaterialInMain flowerMaterialInMain, List<FlowerMaterialIn> flowerMaterialInList) {

		//插入到库存台账
		flowerMaterialBookService.createBooks(flowerMaterialInMain,flowerMaterialInList);
	}
	/**
	 * 采后物资入库主表-库管取消确认
	 * @param flowerMaterialInMain
	 */
	@Override
	public void updateBookSureBack(FlowerMaterialInMain flowerMaterialInMain ) {

		String id = flowerMaterialInMain.getId();
		//删除台账数据
		flowerMaterialBookService.deleteBooks(id);
	}


}
